package com.example.demoddd.domain.userauth.model;

import com.example.demoddd.domain.userauth.model.entity.StUser;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

@Setter
@Getter
public class User implements UserDetails {

    private Long id;
    private String username;
    private String email;
    private String passwordHash;
    //权限集合
    private Collection<String> permissions = new ArrayList<>();

    public static User build(StUser stUser) {
        if (null == stUser) {
            return null;
        }

        User user = new User();
        user.setUsername(stUser.getUsername());
        user.setId(stUser.getUserId());
        user.setPasswordHash(stUser.getPassword());
        user.setEmail(stUser.getEmail());

        return user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        return permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return this.passwordHash;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
